package com.pomelo.tiku.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;


import com.pomelo.tiku.R;
import com.pomelo.tiku.utils.ClickUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuxiangwang
 * @date .
 */
public class AnswerQuesionAdapter extends BaseAdapter<String>{
    private Context mContext;
    private TextView tv_question;
    private TextView txt_submit;
    private TextView txt_resolve;
    private ImageView img_praise;
    private ImageView img_collect;
    private ImageView img_message;
    private LinearLayout ll_answer_contaner;
    private LinearLayout ll_one;
    private LinearLayout ll_two;

    private RadioButton radio_1;
    private RadioButton radio_2;
    private RadioButton radio_3;
    private RadioButton radio_4;

    private int questionType = 0;

    private OnItemViewClick mOnItemViewClick;
    private List<String> list_anwser = new ArrayList<>();
    public AnswerQuesionAdapter(Context context,List<String> list) {
        super(list);
        this.mContext = context;
        list_anwser.add("A static");
        list_anwser.add("B public");
        list_anwser.add("C final");
        list_anwser.add("D abstract");
    }

    public void setOnItemViewClicklistener(OnItemViewClick onItemViewClick) {
        this.mOnItemViewClick = onItemViewClick;
    }

    @Override
    public void onBindView(List<String> list, String data, int pos, RecyclerView.ViewHolder holder, Context context, View view) {
        tv_question = view.findViewById(R.id.txt_question);
        txt_submit = view.findViewById(R.id.txt_submit);
        txt_resolve = view.findViewById(R.id.txt_resolve);

        img_praise = view.findViewById(R.id.img_praise);
        img_collect = view.findViewById(R.id.img_collect);
        img_message = view.findViewById(R.id.img_message);

        tv_question.setText(data);
        ll_answer_contaner = view.findViewById(R.id.ll_answer_container);
        ll_answer_contaner.clearDisappearingChildren();
        addAnserView(list_anwser,ll_answer_contaner);
        txt_submit.setOnClickListener(view1 -> {
            if(ClickUtils.isDoubleClick(txt_submit)) {
                return;
            }
            if(mOnItemViewClick != null) {
                mOnItemViewClick.onSubmitClick(pos);
            }
        });
        txt_resolve.setOnClickListener(view12 -> {
            if(ClickUtils.isDoubleClick(txt_resolve)) {
                return;
            }
            if(mOnItemViewClick != null) {
                mOnItemViewClick.onResolveClick(pos);
            }
        });
        img_praise.setOnClickListener(view12 -> {
            if(ClickUtils.isDoubleClick(img_praise)) {
                return;
            }
            if(mOnItemViewClick != null) {
                mOnItemViewClick.onPraiseClick(pos);
            }
        });
        img_collect.setOnClickListener(view12 -> {
            if(ClickUtils.isDoubleClick(img_collect)) {
                return;
            }
            if(mOnItemViewClick != null) {
                mOnItemViewClick.onCollectClick(pos);
            }
        });
        img_message.setOnClickListener(view12 -> {
            if(ClickUtils.isDoubleClick(img_message)) {
                return;
            }
            if(mOnItemViewClick != null) {
                mOnItemViewClick.onMessageClick(pos);
            }
        });

        initSelectAnwserButton(view,pos);

    }

    private void initSelectAnwserButton(View view,int position) {
        ll_one = view.findViewById(R.id.ll_one);
        ll_two = view.findViewById(R.id.ll_two);

        radio_1 = view.findViewById(R.id.radio_1);
        radio_2 = view.findViewById(R.id.radio_2);
        radio_3 = view.findViewById(R.id.radio_3);
        radio_4 = view.findViewById(R.id.radio_4);

        radio_1.setChecked(false);
        radio_2.setChecked(false);
        radio_3.setChecked(false);
        radio_4.setChecked(false);


            if(0 == position % 3) {
                ll_two.setVisibility(View.VISIBLE);
            } else if(1 == position % 3) {
                ll_two.setVisibility(View.GONE);
            } else if(2 == position % 3) {
                ll_two.setVisibility(View.GONE);
            }


        radio_1.setOnClickListener(view13 -> {
            if(ClickUtils.isDoubleClick(radio_1)) {
                return;
            }
        });
        radio_2.setOnClickListener(view13 -> {
            if(ClickUtils.isDoubleClick(radio_2)) {
                return;
            }

        });
        radio_3.setOnClickListener(view13 -> {
            if(ClickUtils.isDoubleClick(radio_3)) {
                return;
            }

        });
        radio_4.setOnClickListener(view13 -> {
            if(ClickUtils.isDoubleClick(radio_4)) {
                return;
            }

        });
    }

    private void addAnserView(List<String> list,LinearLayout layout) {
       if(list == null) {
           return;
       }
        if(layout.getChildCount() < list.size()) { //防止一直添加view
            for(int index = 0; index < list.size(); index++) {

                View view = LayoutInflater.from(mContext).inflate(R.layout.item_answer_layout,null);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                view.setLayoutParams(params);
                TextView tv_anwser = view.findViewById(R.id.tv_answer);
                tv_anwser.setText(list_anwser.get(index));
                layout.addView(view);
            }
        }

    }

    @Override
    public int setLayout() {
        return R.layout.item_answer_question_layout;
    }

    public interface OnItemViewClick {
        void onSubmitClick(int position);
        void onResolveClick(int position);
        void onPraiseClick(int position);
        void onCollectClick(int position);
        void onMessageClick(int position);
    }
}
